﻿
using System;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.UI.WebControls;
namespace Common
{
    public abstract class JScript
    {
        protected JScript()
        {
        }

        public static void Alert(string message)
        {
            string js = "<script language=javascript>alert('{0}');</script>";
            HttpContext.Current.Response.Write(string.Format(js, message));
        }

        public static void alert_history(string _Msg, int BackLong)
        {
            string StrScript = "<script language=javascript>";
            object CS = StrScript + "alert('" + _Msg + "');";
            StrScript = string.Concat(new object[] { RuntimeHelpers.GetObjectValue(CS), "history.go('", BackLong, "')" }) + "</script>";
            HttpContext.Current.Response.Write(StrScript);
        }

        public static void alert_reloadwin(string _Msg)
        {
            string StrScript = "<script language=javascript>";
            StrScript = (StrScript + "alert('" + _Msg + "');") + "window.opener.location.href=window.opener.location.href;window.close();</script>";
            HttpContext.Current.Response.Write(StrScript);
        }

        public static void AlertAndRedirect(string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }

        public static void AlertAndTopRedirect(string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');window.top.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }

        public static void CloseWindow()
        {
            string js = "<Script language='JavaScript'>window.close();</Script>";
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
        }

        public static void GoHistory(int value)
        {
            string js = "<Script language='JavaScript'>history.go({0});</Script>";
            HttpContext.Current.Response.Write(string.Format(js, value));
        }

        public static string JSStringFormat(string s)
        {
            return s.Replace("\r", @"\r").Replace("\n", @"\n").Replace("'", @"\'").Replace("\"", "\\\"");
        }

        public static void MsgBox(string _Msg)
        {
            string StrScript = "<script language=javascript>";
            StrScript = StrScript + "alert('" + _Msg + "');" + "</script>";
            HttpContext.Current.Response.Write(StrScript);
        }

        public static void MsgBox1(string _Msg, string URL)
        {
            string StrScript = "<script language=javascript>";
            string CS = StrScript;
            StrScript = (CS + "var retValue=window.confirm('" + _Msg + "');if(retValue){window.location='" + URL + "';}") + "</script>";
            HttpContext.Current.Response.Write(StrScript);
        }

        public static void MsgBox2(string _Msg, string URL)
        {
            string StrScript = "<script language=javascript>";
            StrScript = StrScript + "alert('" + _Msg + "');" + "window.location='" + URL + "';" + "</script>";
            HttpContext.Current.Response.Write(StrScript);
        }

        public static void MsgBox3(string _Msg)
        {
            string StrScript = "<script language=javascript>";
            StrScript = (StrScript + "alert('" + _Msg + "');") + "window.close();</script>";
            HttpContext.Current.Response.Write(StrScript);
        }

        public static void OpenWebForm(string url)
        {
            string js = "<Script language='JavaScript'>window.open('" + url + "','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void OpenWebForm(string url, string name, string future)
        {
            string js = "<Script language='JavaScript'>window.open('" + url + "','" + name + "','" + future + "')</Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void Redirect(string toURL)
        {
            string js = "<script language=javascript>window.location.replace('{0}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, toURL));
        }

        public static void RefreshOpener()
        {
            string js = "<Script language='JavaScript'>opener.location.reload();</Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void RefreshParent()
        {
            string js = "<Script language='JavaScript'>parent.location.reload();</Script>";
            HttpContext.Current.Response.Write(js);
        }

        //public static void RtnRltMsgbox(object message, string strWinCtrl)
        //{
        //    string js = StringType.FromObject(ObjectType.StrCatObj(ObjectType.StrCatObj("<Script language='JavaScript'>strWinCtrl = true;strWinCtrl = if(!confirm('", message), "'))return false;</Script>"));
        //    HttpContext.Current.Response.Write(string.Format(js, message.ToString()));
        //}

        public static void ShowConfirm(WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

    }

}